package com.store.bookstore_backend.web.controller;

import com.store.bookstore_backend.dto.APIResponse;
import com.store.bookstore_backend.exception.BookStoreException;
import com.store.bookstore_backend.service.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;

@RestController
public class FileUploadController {
    @Autowired
    private FileStorageService fileStorageService;

    @PostMapping("/upload")
    public APIResponse<Object> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String fileName = fileStorageService.storeFile(file);
            String fileUrl = "/images/" + fileName;  // Nginx静态资源路径

            return APIResponse.success(fileUrl);
        } catch (Exception e) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.ERROR);
        }
    }

}
